<template>
    <mescroll-uni ref="mescrollRef" @init="mescrollInit" :height="height" :down="downOption"
        :up="isClass ? upOption : upOption1" @up="getData" @down="getData" @emptyclick="emptyClick">
        <quick-product-list ref="proList" :isClass="isClass" :orderType="23" :isOnShow="isOnShow" :productList="datas"
            :displayData="displayData" @sku="sku" @amountProp="amountProp" :tempType="tempType" @upFavorite="upFavorite"
            @isUpNum="isUpNum"></quick-product-list>
        <view v-if="!isClass && isNoMore && !getAppSet().pagesClassify">
            <button class="btn btn-main-hollow btn-next-class" @click="nextClass">点击切换到下一分类</button>
        </view>
        <!-- #ifdef H5 -->
        <view class="listMark"></view>
        <!-- #endif -->
    </mescroll-uni>
</template>
<script>
import quickProductList from "@/components/group/quick-product-list.vue"
import { getWholeSaleListForSku } from "@/service/api/product.js"
import utilsOld from "@/utils/utilsOld.js"
import storage from "@/uni-sub-base/utils/storage.js"
export default {
    props: [
        "height",
        "isHideProImg",
        "requestData",
        "infoType",
        "displayData",
        "tempType",
        "isOnShow",
        "isClass",
        "isSearch",
    ],
    components: {
        quickProductList,
    },
    data() {
        return {
            upOption1: {
                textNoMore: "", // 没有更多数据的提示文本
                empty: {
                    use: false,
                },
            },
            downOption: {
                use: true,
            },
            datas: [],
            data: [],
            productList: [],
            param: {},
            priceDecimalPlaces: 2,
            isNoMore: false,
        }
    },
    watch: {
        inputAmount(newVal) {
            this.inputAmount = newVal
        },
    },
    mounted() {
        let productDisplay = storage.getCache("productDisplay")
        if (productDisplay) {
            this.priceDecimalPlaces = productDisplay.PriceDecimalPlaces
        }
    },
    created() {
        this.param = this.requestData
    },
    methods: {
        getData(page) {
            let param = this.param
            if (!this.isSearch && this.isClass && !param.typePath) {
                return
            }
            if (this.isSearch && !param.search) {
                //防搜索页中小程序下加载再次
                return
            }
            param.PageIndex = page.num
            param.PageSize = page.size
            this.isNoMore = false
            getWholeSaleListForSku(param).then((res) => {
                if (param.PageIndex == 1) {
                    this.datas = []
                }
                if (res.Data && res.Data.Data) {
                    this.mescroll.endBySize(res.Data.Data.length, res.Data.RecordAmout)
                    this.datas = this.datas.concat(res.Data.Data)
                    this.changeArr(this.datas, true)
                    if (this.datas == 0) {
                        this.isNoMore = true
                    } else if (this.datas.length != 0 && this.datas.length >= res.Data.RecordAmout) {
                        this.isNoMore = true
                    }
                } else {
                    let arr = []
                    this.mescroll.endBySize(0, 0)
                    this.isNoMore = true
                }
                this.$emit("getDataBack", "")
            })
        },
        // 整理数据
        changeArr(Arr, isNew) {
            const _this = this
            Arr.forEach((item) => {
                // 图片列表
                if (item.ImageUrl) {
                    const ImgUrlList = utilsOld.strToArr(item.ImageUrl)
                    item.logo = ImgUrlList[0]
                }
                if (item.ThumbnailUrl160) {
                    item.logo = item.ThumbnailUrl160
                }

                // 设置显示的动态值
                item.CurrentMaxPrice =
                    item.SaleMaxPrice % 1 === 0
                        ? item.SaleMaxPrice
                        : Number(item.SaleMaxPrice.toFixed(this.priceDecimalPlaces)) // 显示最高价格
                item.CurrentMinPrice =
                    item.SaleMinPrice % 1 === 0
                        ? item.SaleMinPrice
                        : Number(item.SaleMinPrice.toFixed(this.priceDecimalPlaces)) // 显示最低价格
                item.CurrentUnit = item.ProductUnitList[0].UnitName // 显示当前单位
                item.CurrentUnitId = item.ProductUnitList[0].UnitId // 显示当前单位ID
                item.CurrentStock = item.V_StockSum // 显示当前库存
                item.UnitActiveIndex = 0 // 单规格商品默认显示第一个加减框

                // 单位栏添加
                if (item.ProductUnitList.length > 0) {
                    item.ProductUnitList.forEach((ite) => {
                        ite.Title = ite.UnitName + (ite.Ratio !== 1 ? "=" + ite.Ratio + item.CurrentUnit : "")
                        this.initLoopArr(ite.ProductSkuAttrList)
                    })
                }

                // ProductSkuList数据修改
                item.ProductSkuList.forEach((ite) => {
                    if (ite.ProductUnitPriceList[0]) {
                        ite.CurrentPrice =
                            ite.ProductUnitPriceList[0].Price % 1 === 0
                                ? ite.ProductUnitPriceList[0].Price
                                : Number(ite.ProductUnitPriceList[0].Price.toFixed(this.priceDecimalPlaces))
                        ite.CurrentUnit = ite.ProductUnitPriceList[0].UnitName // 显示当前单位
                        ite.CurrentUnitId = ite.ProductUnitPriceList[0].UnitId // 显示当前单位ID
                        ite.UnitActiveIndex = 0 // 单规格商品默认显示第一个加减框
                        ite.ProductUnitPriceList.forEach((it) => {
                            it.Amount = 0
                            it.Title = it.UnitName + (it.Ratio !== 1 ? "=" + it.Ratio + ite.CurrentUnit : "")
                        })
                    }
                })
                if (isNew) {
                    _this.productList.push(item)
                }
            })
        },
        // 遍历规格
        loopArr(arr, skuId = 0, unitId = 0, amount) {
            if (arr && arr.length > 0) {
                for (let i = 0; i < arr.length; i++) {
                    if (arr[i].ProductAttrList && arr[i].ProductAttrList.length > 0) {
                        this.loopArr(arr[i].ProductAttrList, skuId, unitId, amount)
                    } else {
                        if (
                            arr[i].SkuModel &&
                            arr[i].SkuModel.ProductUnitPrice &&
                            skuId === arr[i].SkuModel.ProductUnitPrice.SkuId &&
                            unitId === arr[i].SkuModel.ProductUnitPrice.UnitId
                        ) {
                            arr[i].SkuModel.ProductUnitPrice.Amount = amount
                        }
                    }
                }
            }
        },

        // 遍历规格，数量清零
        initLoopArr(arr) {
            if (arr && arr.length > 0) {
                for (let i = 0; i < arr.length; i++) {
                    if (arr[i].ProductAttrList && arr[i].ProductAttrList.length > 0) {
                        this.initLoopArr(arr[i].ProductAttrList)
                    } else {
                        if (arr[i].SkuModel && arr[i].SkuModel.ProductUnitPrice) {
                            arr[i].SkuModel.ProductUnitPrice.Amount = 0
                        }
                    }
                }
            }
        },
        refresh(requestData) {
            this.datas = []
            if (requestData) {
                this.param = requestData
                this.mescroll.resetUpScroll()
            }
            this.$refs.proList.setUpNumTrue()
        },
        //点击空布局按钮的回调
        emptyClick() {
            uni.showToast({
                title: "点击了按钮,具体逻辑自行实现",
            })
        },
        sku(item) {
            this.$emit("sku", item)
        },
        amountProp(ite, item, type, displayData) {
            this.$emit("amountProp", ite, item, type, displayData)
        },
        upFavorite(e) {
            this.$emit("upFavorite", this.infoType)
        },
        isUpNum(e) {
            this.$emit("isUpNum", e)
        },
        nextClass() {
            this.$emit("loadNoMoreBack", true)
        },
    },
}
</script>

<style lang="scss">
.btn-next-class {
    margin: $app-page-padding;
}

.listMark {
    height: 1rpx;
}
</style>
